package com.ruoyi.common.core.domain.tree;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.annotation.TableField;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.enums.comlbs.OrgTypeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 描述 通用树结构
 * 作者 ZhangPing
 * 创建时间 2023/8/15 14:33
 */
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Data
public class CommonTree<T extends ITree<T>> implements ITree<T> {

    private String id;
    private String parentId;
    @TableField(exist = false)
    private String ancestors;
    private String label;
    private Integer type;
    private List<T> children = new ArrayList<>();

    /**
     * 部门转树对象
     * @param dept
     */
    public CommonTree(SysDept dept) {
        this.id = dept.getDeptId().toString();
        this.parentId = dept.getParentId().toString();
        this.ancestors = dept.getAncestors();
        this.type = dept.getType();
        this.label = dept.getDeptName();
    }

    /**
     * 父类转成字类
     * @param <T>
     * @return
     */
    public <T extends ITree<T>> T toExtentTree(Class<T> clz){
        return JSON.parseObject(JSON.toJSONString(this), clz);
    }

    @Override
    public String id() {
        return id;
    }

    @Override
    public String parentId() {
        return parentId;
    }

    @Override
    public void parentId(String parentId) {
        this.parentId = parentId;
    }

    @Override
    public String ancestors() {
        return ancestors;
    }

    @Override
    public Integer type() {
        return type;
    }

    @Override
    public String label() {
        return label;
    }

    @Override
    public List<T> getChildren() {
        return children;
    }

    @Override
    public void childrenToEmpty() {
        children = new ArrayList<>();
    }
}
